package cn.gov.msa.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

public class GsonHelper {
	private static Gson sGson = new Gson();
	private static JsonParser sJsonParser = new JsonParser();

	/**
	 * 将json数据转化为实体数据
	 * 
	 * @param jsonData
	 *            json字符串
	 * @param entityClass
	 *            类型
	 * @return 实体
	 */
	public static <T> T convertEntity(String jsonData, Class<T> entityClass) {
		T entity = null;
		try {
			entity = sGson.fromJson(jsonData.toString(), entityClass);
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
		}
		return entity;
	}

	/**
	 * 将json数据转化为实体列表数据
	 * 
	 * @param jsonData
	 *            json字符串
	 * @param entityClass
	 *            类型
	 * @return 实体列表
	 */
	public static <T> List<T> convertEntities(String jsonData, Class<T> entityClass) {
		List<T> entities = new ArrayList<T>();
		try {
			JsonArray jsonArray = sJsonParser.parse(jsonData).getAsJsonArray();
			for (JsonElement element : jsonArray) {
				entities.add(sGson.fromJson(element, entityClass));
			}
		} catch (JsonSyntaxException e) {
			e.printStackTrace();
		}
		return entities;
	}

	/**
	 * 将 Object 对象转为 String
	 * 
	 * @param jsonObject
	 *            json对象
	 * @return json字符串
	 */
	public static String object2JsonStr(Object jsonObject) {
		/*String josn=sGson.toJson(jsonObject);*/
		String josn="{\"subjects\":"+sGson.toJson(jsonObject)+"}";
		return josn;
	}

	public static String object2JsonStrForNull(Object jsonObject) {
		Gson sGson = new GsonBuilder().serializeNulls().create();//设置转化json格式后空值会输出
		return sGson.toJson(jsonObject).replace(":null", ":\"\"");//将jsonObject对象转化为json各式后将空值部分替换。
	}

	public String objectJsonStr(Object jsonObject) {

		// Gson gson = new GsonBuilder().serializeNulls().create();
		Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory())
				.setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
		return gson.toJson(jsonObject);
	}

	public class NullStringToEmptyAdapterFactory<T> implements TypeAdapterFactory {
		@SuppressWarnings("unchecked")
		public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
			Class<T> rawType = (Class<T>) type.getRawType();
			if (rawType != String.class) {
				return null;
			}
			return (TypeAdapter<T>) new StringNullAdapter();
		}
	}

	public class StringNullAdapter extends TypeAdapter<String> {
		@Override
		public String read(JsonReader reader) throws IOException {			
			if (reader.peek() == JsonToken.NULL) {
				reader.nextNull();
				return "";
			}
			return reader.nextString();
		}

		@Override
		public void write(JsonWriter writer, String value) throws IOException {			
			if (value == null) {
				writer.nullValue();
				return;
			}
			writer.value(value);
		}
	}
}
